package com.ruoyi.flowable.controller;

import java.util.List;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.vo.FormVersionVo;
import com.ruoyi.flowable.domain.bo.FormVersionBo;
import com.ruoyi.flowable.service.IFormVersionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表单版本
 *
 * @author zxw
 * @date 2023-03-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/formVersion")
public class FormVersionController extends BaseController {

    private final IFormVersionService iFormVersionService;

    /**
     * 查询表单版本列表
     */
    @SaCheckPermission("flowable:formVersion:list")
    @GetMapping("/list")
    public TableDataInfo<FormVersionVo> list(FormVersionBo bo, PageQuery pageQuery) {
        return iFormVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出表单版本列表
     */
    @SaCheckPermission("flowable:formVersion:export")
    @Log(title = "表单版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FormVersionBo bo, HttpServletResponse response) {
        List<FormVersionVo> list = iFormVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "表单版本", FormVersionVo.class, response);
    }

    /**
     * 获取表单版本详细信息
     *
     * @param versionId 主键
     */
    @SaCheckPermission("flowable:formVersion:query")
    @GetMapping("/{versionId}")
    public R<FormVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long versionId) {
        return R.ok(iFormVersionService.queryById(versionId));
    }

    /**
     * 新增表单版本
     */
    @SaCheckPermission("flowable:formVersion:add")
    @Log(title = "表单版本", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FormVersionBo bo) {
        return toAjax(iFormVersionService.insertByBo(bo));
    }

    /**
     * 修改表单版本
     */
    @SaCheckPermission("flowable:formVersion:edit")
    @Log(title = "表单版本", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FormVersionBo bo) {
        return toAjax(iFormVersionService.updateByBo(bo));
    }

    /**
     * 删除表单版本
     *
     * @param versionIds 主键串
     */
    @SaCheckPermission("flowable:formVersion:remove")
    @Log(title = "表单版本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{versionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] versionIds) {
        return toAjax(iFormVersionService.deleteWithValidByIds(Arrays.asList(versionIds), true));
    }
}
